package com.example.administrator.myservice;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnstarService;
    private Button btnstopService;
    private Button btnBindService;
    private Button btnunBindService;
    private ServiceConnection serviceConnection;
    private MyBinder binder;
    private Button btnadd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnstarService=findViewById(R.id.btn_star_service);
        btnstopService=findViewById(R.id.btn_stop_service);
        btnBindService=findViewById(R.id.btn_bind_service);
        btnunBindService=findViewById(R.id.btn_unbind_service);
        btnadd=findViewById(R.id.btn_add);
        /*定义连接器进行绑定*/
        serviceConnection=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder iBinder) {
                Log.e("MainActivity.this","onServiceConnected");
               binder=(MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            Log.e("MainActivity.this","onServiceDisconnected");
            }
        };
        btnstarService.setOnClickListener(new View.OnClickListener() {
            @Override
            /*启动服务*/
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                startService(intent);
            }
        });
        /*取消启动服务*/
        btnstopService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                stopService(intent);
            }
        });
        /*绑定服务*/
       btnBindService.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent();
               intent.setClass(MainActivity.this,MyService.class);
               bindService(intent,serviceConnection,BIND_AUTO_CREATE);
           }
       });
       /*取消绑定服务*/
       btnunBindService.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               unbindService(serviceConnection);
           }
       });
       /*计算*/
       btnadd.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               int result=binder.add(100,200);
               Log.e("binder.add","result="+result);
               String str=binder.toUpperCase("hello");
               Log.e("binder.toUpperCase","str="+str);
               String fomat=binder.getTimestr();
               Log.e("getTimestr","fomat="+fomat);
               String str1=binder.reverse("I love you");
               Log.e("reverse","str1="+str1);
           }
       });
    }
}
